unit TPS_Pindah;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, ZAbstractTable, ZDataset, DB, ZAbstractRODataset,
  ZAbstractDataset, ZConnection, Setting, ExtCtrls;

type
  TPemilih = record
     ID:String;
     NIK:String;
     NKK:String;
     NAMA:String;
     TEMPAT:String;
     TANGGAL:String;
     IBU:String;
     AGAMA:String;
     KAWIN:String;
     KELAMIN:String;
     KELAINAN:String;
     ALAMAT:String;
     KOTA:String;
     KEC:String;
     DESA:String;
     RW:String;
     RT:String;
     KET:String;
  end;
  TTPemilih = array of TPemilih;
  TPenyimpan = record
     ID:String;
     NAMA:String;
  end;
  TTPenyimpan = array of TPenyimpan;
  TDesa = record
     KODE:String;
     NAMA:String;
  end;
  TTDesa = array of TDesa;
  TKec = record
     KODE:String;
     NAMA:String;
  end;
  TTKec = array of TKec;
  TKota = record
     KODE:String;
     NAMA:String;
  end;
  TTKota = array of TKota;
  TTPS_Pindah = class(TForm)
    btnClose: TButton;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    ZTable1: TZTable;
    ZQuery2: TZQuery;
    ZTable2: TZTable;
    Panel1: TPanel;
    title_tps: TLabel;
    Panel2: TPanel;
    btnnext: TButton;
    Btnprev: TButton;
    btnTPS1: TButton;
    lblhal: TLabel;
    lblPemilih: TLabel;
    sgTPS1: TStringGrid;
    txtHal: TEdit;
    Panel3: TPanel;
    btnNext2: TButton;
    btnPrev2: TButton;
    btnTPS2: TButton;
    cbTPS2: TComboBox;
    lblHal2: TLabel;
    lblPemilih2: TLabel;
    sgTPS2: TStringGrid;
    txtHal2: TEdit;
    Label1: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cbTPS2Change(Sender: TObject);
    procedure BtnprevClick(Sender: TObject);
    procedure btnnextClick(Sender: TObject);
    procedure btnPrev2Click(Sender: TObject);
    procedure btnNext2Click(Sender: TObject);
    procedure sgTPS1Click(Sender: TObject);
    procedure sgTPS2Click(Sender: TObject);
    procedure btnTPS1Click(Sender: TObject);
    procedure btnTPS2Click(Sender: TObject);
    procedure txtHalKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure txtHal2KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    procedure TampilHalaman1(Hal:Integer = 1);
    procedure TampilHalaman2(Hal: Integer = 1);
    //procedure DeletePemilih1(var X: TTPemilih; const Index: Integer);
    //procedure DeletePemilih2(var X: TTPemilih; const Index: Integer);
    function getIdsimpan(tabel: String): String;
    //procedure addPemilih(var dari: TTPemilih; ke: TTPemilih;const Index: Integer);
    procedure getPage1;
    procedure getPage2;
    procedure getData1;
    procedure getData2;
    procedure AutoSizeCol(Grid: TStringGrid; Column: integer);

    { Private declarations }
  public
    constructor Create(AOwner: TComponent; kode_desa:String; id_tps: String); overload;
    { Public declarations }
  end;

var
  pindahTps: TTPS_Pindah;
  sKabkot:string;
  skec:string;
  sdesa:string;
  tps1:string;
  tps2:string;
  crow:integer;
  drow:integer;
  cHal:Integer; //current halaman
  jHal:Integer; //jumlah halaman
  bHal:Integer; //batas per halaman
  crow2:integer;
  drow2:integer;
  cHal2:Integer; //current halaman
  jHal2:Integer; //jumlah halaman
  bHal2:Integer; //batas per halaman
  bTPS1:TTPemilih; //grid 1
  bTPS2:TTPemilih; //grid 2
  ct1,ct2:Integer;
  ptable1,ptable2:String;
  pid1,pid2:String;

implementation

{$R *.dfm}
constructor TTPS_Pindah.Create(AOwner: TComponent; kode_desa:String; id_tps: String);
begin
    sdesa:=kode_desa;
    tps1:=id_tps;
    inherited Create(AOwner);
end;

procedure TTPS_Pindah.btnCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TTPS_Pindah.btnNext2Click(Sender: TObject);
begin
  TampilHalaman2(cHal2+1);
  txtHal2.Text:=IntToStr(cHal2);
end;

procedure TTPS_Pindah.btnnextClick(Sender: TObject);
begin
  TampilHalaman1(cHal+1);
  txtHal.Text:=IntToStr(cHal);
end;

procedure TTPS_Pindah.btnPrev2Click(Sender: TObject);
begin
  TampilHalaman2(cHal2-1);
  txtHal2.Text:=IntToStr(cHal2);
end;

procedure TTPS_Pindah.BtnprevClick(Sender: TObject);
begin
  TampilHalaman1(cHal-1);
  txtHal.Text:=IntToStr(cHal);
end;

procedure TTPS_Pindah.btnTPS1Click(Sender: TObject);
var
  kodpem:String;
  pos:Integer;
begin
  //ubah yang ada di tabel lama
  if (tps2<>'') and (ct2<600) then begin
    pos:=sgTPS1.Row;
    kodpem:=pid1;
    with ZQuery1 do
    begin
      Active:=False;
      sql.Clear;
      sql.Text:='UPDATE pemilih_'+sdesa+' SET TPS='+tps2+' WHERE ID_USER='+QuotedStr(kodpem);
//      showmessage('update'+sdesa+', '+tps2+', '+kodpem);
      ExecSQL;
    end;
    //tambah tampilan tps2
    getData1;
    getData2;
    TampilHalaman1(chal);
    TampilHalaman2(chal2);
  end
    else
  ShowMessage('Pilih dahulu TPS 2 dengan kondisi sesuai');
end;

procedure TTPS_Pindah.btnTPS2Click(Sender: TObject);
var
  kodpem:String;
  pos:Integer;
begin
  //ubah yang ada di tabel lama
//  ShowMessage(tps1+' '+kodpem);
  if (tps1<>'') and (ct1<600) then begin
    pos:=sgTPS2.Row;
    kodpem:=pid2;
    with ZQuery1 do
    begin
      Active:=False;
      sql.Clear;
      sql.Text:='UPDATE pemilih_'+sdesa+' SET TPS='+tps1+' WHERE ID_USER='+QuotedStr(kodpem);
      ExecSQL;
    end;
    getData1;
    getData2;
    TampilHalaman1(chal);
    TampilHalaman2(chal2);
  end
    else
  ShowMessage('Pilih dahulu TPS 1 dengan kondisi sesuai');
end;

{procedure TeditTps.DeletePemilih1(var X: TTPemilih; const Index: Integer) ;
begin
   if Index > High(X) then Exit;
   if Index < Low(X) then Exit;
   if Index = High(X) then
   begin
     SetLength(X, Length(X) - 1) ;
     Exit;
   end;
   Finalize(X[Index]) ;
   System.Move(X[Index+1], X[Index],(Length(X) - Index -1) * SizeOf(TPemilih) + 1) ;
   SetLength(X, Length(X) - 1);
   drow:=drow-1;
   sgTPS1.RowCount:=sgTPS1.RowCount-1;
end;

procedure TeditTps.addPemilih(var dari: TTPemilih; ke: TTPemilih; const Index: Integer);
var
  n:Integer;
begin
  n:=Length(ke);
  SetLength(ke,n+1);
  ke[n]:=dari[Index];
  if (dari=bTPS1) then
    drow:=drow+1
  else
    drow2:=drow2+1;
end;

procedure TeditTps.DeletePemilih2(var X: TTPemilih; const Index: Integer) ;
begin
   if Index > High(X) then Exit;
   if Index < Low(X) then Exit;
   if Index = High(X) then
   begin
     SetLength(X, Length(X) - 1) ;
     Exit;
   end;
   Finalize(X[Index]) ;
   System.Move(X[Index+1], X[Index],(Length(X) - Index -1) * SizeOf(TPemilih) + 1) ;
   SetLength(X, Length(X) - 1);
   drow2:=drow2-1;
   sgTPS2.RowCount:=sgTPS2.RowCount-1;
end; }

procedure TTPS_Pindah.TampilHalaman2(Hal:Integer = 1);
var
  iter:Integer;
  query:String;
  kKota:String;
  kKec:String;
  kDesa:String;
  tblname,kode,tpemilih,ttps:String;
  iduser,kodpem:String;
  i,awal:Integer;
  vRow:Integer;
begin
  dRow2:=1;
  vRow:=1;
  cHal2:=Hal;
  sgTPS2.RowCount:=2;
  lblHal2.Caption:='-';
  //reset
  for i := 1 to sgTPS2.RowCount - 1
  do sgTPS2.Rows[i].Clear;
  //
  query:='TPS ='+tps2;
  ZQuery2.Active := False;
  awal:=(Hal-1)*bhal;
  ttps:='data_tps_'+sdesa;
  tpemilih:='pemilih_'+sdesa;
  //query:= 'SELECT * FROM '+ttps+' LEFT JOIN '+tpemilih+' ON '+ttps+'.KODE_PEMILIH='+tpemilih+'.ID_USER  WHERE '+query+' LIMIT '+IntToStr(awal)+','+IntToStr(bhal);
  query:= 'SELECT * FROM '+tpemilih+' WHERE '+query+' LIMIT '+IntToStr(awal)+','+IntToStr(bhal);
  ZQuery2.SQL.Text := query;
  ZQuery2.Active := True;
  ZQuery2.First;  //tabel simpan
  //get desa,kota,kec
  if (sDesa<>'') then
    begin
//      kKota:=cbKabkot.Items[cbKabkot.ItemIndex];
//      kKec:=cbKec.Items[cbKec.ItemIndex];
//      kDesa:=cbDesa.Items[cbDesa.ItemIndex];
    end;
  //
  while (not ZQuery2.Eof) do begin
    Inc(drow2);
    SetLength(bTPS2,dRow2);
    sgTPS2.RowCount:=dRow2;
      bTPS2[vRow].ID :='pemilih_'+sdesa+ZQuery2.Fields[0].AsString;
      bTPS2[vRow].NIK:=ZQuery2.FieldByName('NIK').AsString;
      bTPS2[vRow].NKK:=ZQuery2.FieldByName('NKK').AsString;
      bTPS2[vRow].NAMA:=ZQuery2.FieldByName('NAMA').AsString;
      bTPS2[vRow].TEMPAT:=ZQuery2.FieldByName('TEMPAT').AsString;
      bTPS2[vRow].TANGGAL:=ZQuery2.FieldByName('TANGGAL').AsString;
      bTPS2[vRow].IBU:=ZQuery2.FieldByName('IBU').AsString;
      bTPS2[vRow].AGAMA:=ZQuery2.FieldByName('AGAMA').AsString;
      bTPS2[vRow].KAWIN:=ZQuery2.FieldByName('KAWIN').AsString;
      bTPS2[vRow].KELAMIN:=ZQuery2.FieldByName('KELAMIN').AsString;
      bTPS2[vRow].KELAINAN:=ZQuery2.FieldByName('KELAINAN').AsString;
      bTPS2[vRow].ALAMAT:=ZQuery2.FieldByName('ALAMAT').AsString;
      bTPS2[vRow].KOTA:=kKota;
      bTPS2[vRow].KEC:=kKec;
      bTPS2[vRow].DESA:=kDesa;
      bTPS2[vRow].RW:=ZQuery2.FieldByName('RW').AsString;
      bTPS2[vRow].RT:=ZQuery2.FieldByName('RT').AsString;
      bTPS2[vRow].KET:=ZQuery2.FieldByName('KET').AsString;
      //
      sgTPS2.Cells[0,vRow]:=IntToStr((chal2-1)*bhal+vRow);
      sgTPS2.Cells[1,vRow]:=bTPS2[vRow].NIK;
      sgTPS2.Cells[2,vRow]:=bTPS2[vRow].NKK;
      sgTPS2.Cells[3,vRow]:=bTPS2[vRow].NAMA;
      sgTPS2.Cells[4,vRow]:=bTPS2[vRow].TEMPAT;
      sgTPS2.Cells[5,vRow]:=bTPS2[vRow].TANGGAL;
      sgTPS2.Cells[6,vRow]:=bTPS2[vRow].IBU;
      sgTPS2.Cells[7,vRow]:=bTPS2[vRow].AGAMA;
      sgTPS2.Cells[8,vRow]:=bTPS2[vRow].KAWIN;
      sgTPS2.Cells[9,vRow]:=bTPS2[vRow].KELAMIN;
      sgTPS2.Cells[10,vRow]:=bTPS2[vRow].KELAINAN;
      sgTPS2.Cells[11,vRow]:=bTPS2[vRow].ALAMAT;
      sgTPS2.Cells[12,vRow]:=bTPS2[vRow].KOTA;
      sgTPS2.Cells[13,vRow]:=bTPS2[vRow].KEC;
      sgTPS2.Cells[14,vRow]:=bTPS2[vRow].DESA;
      sgTPS2.Cells[15,vRow]:=bTPS2[vRow].RW;
      sgTPS2.Cells[16,vRow]:=bTPS2[vRow].RT;
      sgTPS2.Cells[17,vRow]:=bTPS2[vRow].KET;
    vRow:=vRow+1;
    ZQuery2.Next;
  end;
  lblHal2.Caption := IntToStr(Hal)+' of '+IntToStr(jHal2);
  //-----------------------------------------
  txtHal2.Enabled:=True;
  if (cHal2=1) then
    btnPrev2.Enabled:=False
  else
    btnPrev2.Enabled:=True;
  if (cHal2=jHal2) then
    btnNext2.Enabled:=False
  else
    btnNext2.Enabled:=True;
  for i := 0 to sgTPS2.ColCount - 1 do
    AutoSizeCol(sgTPS2, i);
end;

procedure TTPS_Pindah.txtHal2KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if key = VK_RETURN then
    getPage2;
end;

procedure TTPS_Pindah.getPage2();
begin
    if ((txtHal2.Text='') or (StrToInt(txtHal2.Text)>jHal) or (StrToInt(txtHal2.Text)<1)) then
      ShowMessage('Isi dahulu dengan halaman yang sesuai')
    else if (jHal2=0) then
      ShowMessage('Tidak ada halaman yang dapat ditampilkan')
    else
      begin
        TampilHalaman2(StrToInt(txtHal2.Text));
      end;
end;

procedure TTPS_Pindah.txtHalKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if key = VK_RETURN then
    getPage1;
end;

procedure TTPS_Pindah.getPage1();
begin
    if ((txtHal.Text='') or (StrToInt(txtHal.Text)>jHal) or (StrToInt(txtHal.Text)<1)) then
      ShowMessage('Isi dahulu dengan halaman yang sesuai')
    else if (jHal=0) then
      ShowMessage('Tidak ada halaman yang dapat ditampilkan')
    else
      begin
        TampilHalaman1(StrToInt(txtHal.Text));
      end;
end;

procedure TTPS_Pindah.TampilHalaman1(Hal:Integer = 1);
var
  iter:Integer;
  query:String;
  kKota:String;
  kKec:String;
  kDesa:String;
  tblname,kode,tpemilih,ttps:String;
  iduser,kodpem:String;
  i,awal:Integer;
  vRow:Integer;
begin
  dRow:=1;
  vRow:=1;
  cHal:=Hal;
  sgTPS1.RowCount:=2;
  lblHal.Caption:='-';
  //reset
  for i := 1 to sgTPS1.RowCount - 1
  do sgTPS1.Rows[i].Clear;
  //
  query:='TPS ='+tps1;
  ZQuery2.Active := False;
  awal:=(Hal-1)*bhal;
  ttps:='data_tps_'+sdesa;
  tpemilih:='pemilih_'+sdesa;
  //query:= 'SELECT * FROM '+ttps+' LEFT JOIN '+tpemilih+' ON '+ttps+'.KODE_PEMILIH='+tpemilih+'.ID_USER  WHERE '+query+' LIMIT '+IntToStr(awal)+','+IntToStr(bhal);
  query:= 'SELECT * FROM '+tpemilih+' WHERE '+query+' LIMIT '+IntToStr(awal)+','+IntToStr(bhal);
  ZQuery2.SQL.Text := query;
  ZQuery2.Active := True;
  ZQuery2.First;  //tabel simpan
  //get desa,kota,kec
  if (sDesa<>'') then
    begin
//      kKota:=cbKabkot.Items[cbKabkot.ItemIndex];
//      kKec:=cbKec.Items[cbKec.ItemIndex];
//      kDesa:=cbDesa.Items[cbDesa.ItemIndex];
    end;
  //
  while (not ZQuery2.Eof) do begin
    Inc(drow);
    SetLength(bTPS1,dRow);
    sgTPS1.RowCount:=dRow;
      bTPS1[vRow].ID :='pemilih_'+sdesa+ZQuery2.FieldByName('ID_USER').AsString;
      //bTPS1[vRow].ID :='pemilih_'+sdesa+ZQuery2.Fields[2].AsString;
      bTPS1[vRow].NIK:=ZQuery2.FieldByName('NIK').AsString;
      bTPS1[vRow].NKK:=ZQuery2.FieldByName('NKK').AsString;
      bTPS1[vRow].NAMA:=ZQuery2.FieldByName('NAMA').AsString;
      bTPS1[vRow].TEMPAT:=ZQuery2.FieldByName('TEMPAT').AsString;
      bTPS1[vRow].TANGGAL:=ZQuery2.FieldByName('TANGGAL').AsString;
      bTPS1[vRow].IBU:=ZQuery2.FieldByName('IBU').AsString;
      bTPS1[vRow].AGAMA:=ZQuery2.FieldByName('AGAMA').AsString;
      bTPS1[vRow].KAWIN:=ZQuery2.FieldByName('KAWIN').AsString;
      bTPS1[vRow].KELAMIN:=ZQuery2.FieldByName('KELAMIN').AsString;
      bTPS1[vRow].KELAINAN:=ZQuery2.FieldByName('KELAINAN').AsString;
      bTPS1[vRow].ALAMAT:=ZQuery2.FieldByName('ALAMAT').AsString;
      bTPS1[vRow].KOTA:=kKota;
      bTPS1[vRow].KEC:=kKec;
      bTPS1[vRow].DESA:=kDesa;
      bTPS1[vRow].RW:=ZQuery2.FieldByName('RW').AsString;
      bTPS1[vRow].RT:=ZQuery2.FieldByName('RT').AsString;
      bTPS1[vRow].KET:=ZQuery2.FieldByName('KET').AsString;
      //
      sgTPS1.Cells[0,vRow]:=IntToStr((chal-1)*bhal+vRow);
      sgTPS1.Cells[1,vRow]:=bTPS1[vRow].NIK;
      sgTPS1.Cells[2,vRow]:=bTPS1[vRow].NKK;
      sgTPS1.Cells[3,vRow]:=bTPS1[vRow].NAMA;
      sgTPS1.Cells[4,vRow]:=bTPS1[vRow].TEMPAT;
      sgTPS1.Cells[5,vRow]:=bTPS1[vRow].TANGGAL;
      sgTPS1.Cells[6,vRow]:=bTPS1[vRow].IBU;
      sgTPS1.Cells[7,vRow]:=bTPS1[vRow].AGAMA;
      sgTPS1.Cells[8,vRow]:=bTPS1[vRow].KAWIN;
      sgTPS1.Cells[9,vRow]:=bTPS1[vRow].KELAMIN;
      sgTPS1.Cells[10,vRow]:=bTPS1[vRow].KELAINAN;
      sgTPS1.Cells[11,vRow]:=bTPS1[vRow].ALAMAT;
      sgTPS1.Cells[12,vRow]:=bTPS1[vRow].KOTA;
      sgTPS1.Cells[13,vRow]:=bTPS1[vRow].KEC;
      sgTPS1.Cells[14,vRow]:=bTPS1[vRow].DESA;
      sgTPS1.Cells[15,vRow]:=bTPS1[vRow].RW;
      sgTPS1.Cells[16,vRow]:=bTPS1[vRow].RT;
      sgTPS1.Cells[17,vRow]:=bTPS1[vRow].KET;
    vRow:=vRow+1;
    ZQuery2.Next;
  end;
  lblHal.Caption := IntToStr(Hal)+' of '+IntToStr(jHal);
  //-----------------------------------------
  txtHal.Enabled:=True;
  if (cHal=1) then
    btnPrev.Enabled:=False
  else
    btnPrev.Enabled:=True;
  if (cHal=jHal) then
    btnNext.Enabled:=False
  else
    btnNext.Enabled:=True;
  for i := 0 to sgTPS1.ColCount - 1 do
    AutoSizeCol(sgTPS1, i);
end;

procedure TTPS_Pindah.AutoSizeCol(Grid: TStringGrid; Column: integer);
var
  i, W, WMax: integer;
begin
  WMax := 0;
  for i := 0 to (Grid.RowCount - 1) do begin
    W := Grid.Canvas.TextWidth(Grid.Cells[Column, i]);
    if W > WMax then
      WMax := W;
  end;
  Grid.ColWidths[Column] := WMax + 5;
end;

procedure TTPS_Pindah.getData1();
var
  pilih:String;
  i:Integer;
  query:String;
begin

    //-------------
    query:='TPS ='+tps1;
    ZQuery2.Active := False;
    query:= 'SELECT COUNT(*) FROM pemilih_'+sdesa+' WHERE '+query;
    ZQuery2.SQL.Text := query;
    ZQuery2.Active := True;
    ZQuery2.First;  //tabel simpan
    ct1:=ZQuery2.Fields[0].AsInteger;
    jHal:=ct1 div bHal;
    if ((ct1 mod bHal)>0) then jHal:=jHal+1;
    lblPemilih.Caption:='Jumlah ='+IntToStr(ct1);
end;

procedure TTPS_Pindah.getData2();
var
  pilih:String;
  i:Integer;
  query:String;
begin
  //get TPS id
  pilih := cbTPS2.Items[cbTPS2.ItemIndex];
  ZTable1.Active := False;
  ZTable1.TableName := 'daftar_tps';
  ZTable1.Filter := 'NAMA = '+QuotedStr(pilih)+' AND KODE_DESA = '+QuotedStr(sdesa);
  ZTable1.Filtered := True;
  ZTable1.Active := True;
  ZTable1.First;
  tps2 := ZTable1.FieldByName('ID_TPS').AsString;
  //-------------
  query:='TPS ='+tps2;
  ZQuery2.Active := False;
  query:= 'SELECT COUNT(*) FROM pemilih_'+sdesa+' WHERE '+query;
  ZQuery2.SQL.Text := query;
  ZQuery2.Active := True;
  ZQuery2.First;  //tabel simpan
  ct2:=ZQuery2.Fields[0].AsInteger;
  jHal2:=ct2 div bHal;
  if ((ct2 mod bHal)>0) then jHal2:=jHal2+1;
  lblPemilih2.Caption:='Jumlah ='+IntToStr(ct2);
end;

procedure TTPS_Pindah.cbTPS2Change(Sender: TObject);
var
  i:Integer;
begin
  if (cbTPS2.ItemIndex > 0) then
  begin
    getData2;
    //
    TampilHalaman2();
  end
    else
  begin
    tps2:='';
    for i := 1 to sgTPS2.RowCount - 1
    do sgTPS2.Rows[i].Clear;
    SetLength(bTPS2,2);
  end;
end;

procedure TTPS_Pindah.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;


procedure TTPS_Pindah.FormCreate(Sender: TObject);
var setting:TSetting_Form;
begin
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.LoginPrompt:=False;
  ZConnection1.Connected := True;
  ZTable1.Connection:=ZConnection1;
  ZTable2.Connection:=ZConnection1;
  ZQuery1.Connection:=ZConnection1;
  ZQuery2.Connection:=ZConnection1;

  
  with ZTable1 do
  begin
    active:=false;
    tablename:='daftar_tps';
    filter:='ID_TPS='+tps1+' AND KODE_DESA='+QuotedStr(sdesa);
    filtered:=true;
    active:=true;
    first;
    title_tps.Caption:='Pindahkan orang dari TPS '+FieldByName('NAMA').AsString;
  end;

  cbTPS2.Clear;
  cbTPS2.Items.Add('Pilih TPS...');
  with ZTable1 do
  begin
    active:=false;
    tablename:='daftar_tps';
    filter:='KODE_DESA='+QuotedStr(sdesa);
    filtered:=true;
    active:=true;
    first;
    while (not eof) do
    begin
      if(FieldByName('ID_TPS').AsString<>tps1)then
      cbtps2.Items.Add(FieldByName('NAMA').AsString);
      next;
    end;
  end;

  cRow:=2;  //wadah row
  crow2:=2;
  dRow:=1;  //menyimpan banyaknya data
  drow2:=1;
  bHal:=20;  //pemilih per halaman
  lblHal.Caption:= '-';
  lblHal2.Caption:= '-';
  lblPemilih2.Caption:='';
  lblPemilih.Caption:='';
  txtHal.Enabled:=False;
  txtHal2.Enabled:=False;
  btnPrev.Enabled:=False;
  btnPrev2.Enabled:=False;
  btnNext.Enabled:=False;
  btnNext2.Enabled:=False;
  //grid 1
  sgTPS1.Cells[0,0]:='NO';
  sgTPS1.Cells[1,0]:='NIK';
  sgTPS1.Cells[2,0]:='NKK';
  sgTPS1.Cells[3,0]:='Nama';
  sgTPS1.Cells[4,0]:='Tempat Lahir';
  sgTPS1.Cells[5,0]:='Tanggal Lahir';
  sgTPS1.Cells[6,0]:='Nama Ibu';
  sgTPS1.Cells[7,0]:='Agama';
  sgTPS1.Cells[8,0]:='Perkawinan';
  sgTPS1.Cells[9,0]:='Jenis Kelamin';
  sgTPS1.Cells[10,0]:='Kelainan';
  sgTPS1.Cells[11,0]:='Alamat';
  sgTPS1.Cells[12,0]:='Kota';
  sgTPS1.Cells[13,0]:='Kecamatan';
  sgTPS1.Cells[14,0]:='Desa';
  sgTPS1.Cells[15,0]:='RW';
  sgTPS1.Cells[16,0]:='RT';
  sgTPS1.Cells[17,0]:='Ket';
  sgTPS1.RowCount:=cRow;
  //grid 2
  sgTPS2.Cells[0,0]:='NO';
  sgTPS2.Cells[1,0]:='NIK';
  sgTPS2.Cells[2,0]:='NKK';
  sgTPS2.Cells[3,0]:='Nama';
  sgTPS2.Cells[4,0]:='Tempat Lahir';
  sgTPS2.Cells[5,0]:='Tanggal Lahir';
  sgTPS2.Cells[6,0]:='Nama Ibu';
  sgTPS2.Cells[7,0]:='Agama';
  sgTPS2.Cells[8,0]:='Perkawinan';
  sgTPS2.Cells[9,0]:='Jenis Kelamin';
  sgTPS2.Cells[10,0]:='Kelainan';
  sgTPS2.Cells[11,0]:='Alamat';
  sgTPS2.Cells[12,0]:='Kota';
  sgTPS2.Cells[13,0]:='Kecamatan';
  sgTPS2.Cells[14,0]:='Desa';
  sgTPS2.Cells[15,0]:='RW';
  sgTPS2.Cells[16,0]:='RT';
  sgTPS2.Cells[17,0]:='Ket';
  sgTPS2.RowCount:=cRow2;
  //tambah buffer
  tps2:='';
  ct2:=0;
  getData1;
  TampilHalaman1();
end;

procedure TTPS_Pindah.sgTPS1Click(Sender: TObject);
var
  pos, tots:Integer;
begin
  if ((sgTPS1.Row>0)and(tps2<>'')) then begin
      btnTPS1.Enabled:=True;
      pos:=sgTPS1.Row;
      //getID
      ptable1:=Copy(bTPS1[pos].ID,1,16);
      tots:=Length(bTPS1[pos].ID)-16;
      pid1:=Copy(bTPS1[pos].ID,17,tots);
//      ShowMessage(IntToStr(pos)+' '+pid1);
  end;
end;

procedure TTPS_Pindah.sgTPS2Click(Sender: TObject);
var
  pos, tots:Integer;
begin
  if (sgTPS2.Row>0) then begin
      btnTPS2.Enabled:=True;
      pos:=sgTPS2.Row;
      //getID
      ptable2:=Copy(bTPS2[pos].ID,1,16);
      tots:=Length(bTPS2[pos].ID)-16;
      pid2:=Copy(bTPS2[pos].ID,17,tots);
//      ShowMessage(IntToStr(pos)+' '+pid2);
  end;
end;

function TTPS_Pindah.getIdsimpan(tabel:String):String;
begin
  with ZTable1 do
  begin
    Active:=False;
    TableName:='penyimpan';
    Filter:='NAMA='+QuotedStr(tabel);
    Filtered:=True;
    Active:=True;
    First;
    Result:=FieldByName('ID').AsString;
  end;
end;

end.

